package com.ziq.zcamp.post

import android.graphics.Canvas
import android.view.View
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.ziq.base.extension.BindingViewHolder

class RecyclerViewDragDeleteHelper<E, F : ViewBinding>(val adapter : RecyclerViewDragAdapter<BindingViewHolder<F>, E, F>, val delArea : View, var callback: DragStateCallback?) : ItemTouchHelper.Callback() {

    private var mIsInside = false
    private var delPos = -1
    private var tempHolder: RecyclerView.ViewHolder? = null

    override fun getMovementFlags(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ): Int {
        //允许上下左右的拖动
        var dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
        //任何方向不响应
        val swipeFlags = 0
        val position = viewHolder.bindingAdapterPosition
        if(!adapter.canDrag(position)){
            dragFlags = 0
        }
        return makeMovementFlags(dragFlags, swipeFlags)
    }

    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        val fromPosition = viewHolder.bindingAdapterPosition //得到item原来的position
        val toPosition = target.bindingAdapterPosition //得到目标position
        if(adapter.canSwap(fromPosition, toPosition)){
            adapter.swap(fromPosition, toPosition)
        }
        return true
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
    }

    override fun getAnimationDuration(
        recyclerView: RecyclerView,
        animationType: Int,
        animateDx: Float,
        animateDy: Float
    ): Long {
        return if (mIsInside) {
            0
        } else super.getAnimationDuration(recyclerView, animationType, animateDx, animateDy)
    }

    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {

        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            // 拖动中
            delPos = viewHolder!!.bindingAdapterPosition
            tempHolder = viewHolder
            callback?.onDrag(tempHolder,true, false)
        } else {
            // 空闲状态
            val isRemovable = callback?.isRemovable(delPos, mIsInside)!!
            callback?.onDrag(tempHolder, false, mIsInside && delPos >= 0 && isRemovable)
            if (mIsInside && delPos >= 0 && isRemovable) {
                adapter.remove(delPos)
                callback?.afterRemove()
            }
            mIsInside = false
            delPos = -1
            tempHolder = null
        }
        super.onSelectedChanged(viewHolder, actionState)
    }

    override fun clearView(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ) {
        //恢复可见性，不然重用有问题
//                viewHolder.itemView.visibility = View.VISIBLE
        //在holder onBindViewHolder 恢复显示
        super.clearView(recyclerView, viewHolder)
    }

    override fun onChildDraw(
        c: Canvas,
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        dX: Float,
        dY: Float,
        actionState: Int,
        isCurrentlyActive: Boolean
    ) {

        val delAreaWidth: Int = delArea.getWidth()
        val delAreaHeight: Int = delArea.getHeight()

        val delLocation = IntArray(2)
        delArea.getLocationInWindow(delLocation)
        val delAreaX = delLocation[0]
        val delAreaY = delLocation[1]


        val itemWidth = viewHolder.itemView.width
        val itemHeight = viewHolder.itemView.height
        val itemLocation = IntArray(2)
        viewHolder.itemView.getLocationInWindow(itemLocation)
        val itemX = itemLocation[0]
        val itemY = itemLocation[1]



        var isInside = callback?.isInside(itemX , itemY , itemWidth, itemHeight,
            delAreaX , delAreaY , delAreaWidth, delAreaHeight)
        if(isInside == null){
            val centerX: Int = itemX + itemWidth / 2
            val centerY: Int = itemY + itemHeight / 2
            isInside = delAreaY < centerY  && centerY < delAreaY + delAreaHeight
                    && delAreaX < centerX  && centerX < delAreaX + delAreaWidth
        }

        callback?.onDraw(viewHolder, isInside)
        mIsInside = isInside
        super.onChildDraw(
            c,
            recyclerView,
            viewHolder,
            dX,
            dY,
            actionState,
            isCurrentlyActive
        )
    }

    interface DragStateCallback {
        fun isInside(itemX :Int, itemY : Int, itemWidth: Int, itemHeight: Int,
                     delAreaX :Int, delAreaY : Int, delAreaWidth: Int, delAreaHeight: Int,
        ) : Boolean? {
            return null
        }
        fun onDrag(viewHolder: RecyclerView.ViewHolder? , isDragging: Boolean, isDelete: Boolean){}
        fun onDraw(viewHolder: RecyclerView.ViewHolder , isInside: Boolean){}
        fun isRemovable(position: Int, isInside: Boolean) : Boolean{
            return true
        }
        fun afterRemove(){}
    }

}